package com.stylefeng.guns.modular.netcar.service.impl;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.stylefeng.guns.core.model.CommonKeyValue;
import com.stylefeng.guns.core.model.Pager;
import com.stylefeng.guns.core.util.JwtTokenUtil;
import com.stylefeng.guns.modular.netcar.dao.LineTimeSlotMapper;
import com.stylefeng.guns.modular.netcar.model.LineCity;
import com.stylefeng.guns.modular.netcar.model.LineTimeSlot;
import com.stylefeng.guns.modular.netcar.model.LineTimeTemplate;
import com.stylefeng.guns.modular.netcar.model.OrderLine;
import com.stylefeng.guns.modular.netcar.service.ILineTimeSlotService;
import com.stylefeng.guns.modular.netcar.service.ILineTimeTemplateService;
import com.stylefeng.guns.modular.netcar.service.IOrderLineService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import com.baomidou.mybatisplus.toolkit.IdWorker;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.*;


/**

 * 线路发车时刻排班 Service 实现 
 * @author 张亚林
 * @Date 2018-10-27 09:55
 */
@Service
public class LineTimeSlotServiceImpl extends ServiceImpl<LineTimeSlotMapper,LineTimeSlot> implements ILineTimeSlotService {

    @Resource
    private LineTimeSlotMapper lineTimeSlotMapper;
    @Autowired
    private IOrderLineService orderLineService;
    @Autowired
    private ILineTimeTemplateService lineTimeTemplateService;
    /**
     * 删除线路发车时刻排班
     * @param lineTimeSlot
     * @return     
     */
    @Transactional
    public int deleteLineTimeSlot(LineTimeSlot lineTimeSlot,String token)throws Exception{
        if(lineTimeSlot==null ){
            throw new RuntimeException("参数不全！");
        }
        return this.lineTimeSlotMapper.deleteLineTimeSlot(lineTimeSlot);
    }
    /**
     * 批量删除线路发车时刻排班
     * @param ids 主键ID数组
     * @return 
     */
    @Transactional
    public int deleteBatchLineTimeSlot(String[] ids,String token)throws Exception{
        return this.lineTimeSlotMapper.deleteBatchLineTimeSlot(ids);
    }
    /**
     * 查询线路发车时刻排班列表
     * @param lineTimeSlot 线路发车时刻排班对象
     * @return
     */
    public List<LineTimeSlot> selectListLineTimeSlot(LineTimeSlot lineTimeSlot,String token)throws Exception{
        return  this.lineTimeSlotMapper.selectListLineTimeSlot(lineTimeSlot);
    }

    @Override
    public List<LineTimeSlot> selectListLineTimeSlotForUpdate(LineTimeSlot lineTimeSlot)throws Exception{
        return this.lineTimeSlotMapper.selectListLineTimeSlotForUpdate(lineTimeSlot);
    }

    /**
	 * 分页查询线路发车时刻排班列表
     * @param pager 分页对象
     * @return
     */     
    public  Map pageListLineTimeSlot(Pager pager,String token) throws Exception{
        Page page=new Page(pager.getPageNumber(),pager.getPageSize());
        page.setRecords(lineTimeSlotMapper.selectListLineTimeSlot(page,(LineTimeSlot)pager.getObj()));
        List<LineTimeSlot> list = page.getRecords();
        Map mapDate = new HashMap();
        mapDate.put("rows", list);
        mapDate.put("total", page.getTotal());
        mapDate.put("page", page.getCurrent());
        mapDate.put("pageCount", page.getPages());
        return mapDate;
    }

    /**
     * 通过主键id查询的线路发车时刻排班列表
     * @param id 主键id
     * @return     
     */
    public  LineTimeSlot findLineTimeSlotById(String id,String token)throws Exception{
        return  this.lineTimeSlotMapper.findLineTimeSlotById(id);
    }

    /**
     * 新增线路发车时刻排班数据
     * @param lineTimeSlot  线路发车时刻排班对象
     * @return
     */
    @Transactional
    public int insertLineTimeSlot(LineTimeSlot lineTimeSlot,String token) throws Exception { 
        if(lineTimeSlot!=null && (lineTimeSlot.getLtsId()==null || lineTimeSlot.getLtsId().trim().length()<10)){
            lineTimeSlot.setLtsId(IdWorker.get32UUID());
        }
        checkData(null,lineTimeSlot.getOlId());
        return this.lineTimeSlotMapper.insertLineTimeSlot(lineTimeSlot);
    }

    /**
     * 根据模板批量新增线路发车时刻排班数据
     * @param olId
     * @param sdate
     * @param edate
     * @return
     */
    @Transactional
    public int insertBatchLineTimeSlot(String olId, Date sdate, Date edate, String token)throws Exception {
        LineTimeSlot lineTimeSlot=new LineTimeSlot();
        lineTimeSlot.setOlId(olId);
        lineTimeSlot.setCreateTime(new Date());
        lineTimeSlot.setCreateUser(JwtTokenUtil.getClaimFromToken(token).getSubject());
        Calendar date = Calendar.getInstance();
        for(;sdate.compareTo(edate)<= 0;){
            lineTimeSlot.setLtsDate(sdate);
            this.lineTimeSlotMapper.insertBacthLineTimeSlot(lineTimeSlot);
            date.setTime(sdate);
            date.set(Calendar.DAY_OF_MONTH, date.get(Calendar.DAY_OF_MONTH) + 1);
            sdate=date.getTime();
        }
        return 0;
    }

    private void checkData(String lttId,String olId)throws Exception{
        if(olId!=null){
            OrderLine orderLine=orderLineService.findOrderLineById(olId,null);
            if(orderLine==null || orderLine.getOlIsAlived().equals("0")){
                throw new RuntimeException("线路不存在或者已经禁用！");
            }
        }
    }
    /**
     * 更新线路发车时刻排班数据
     * @param lineTimeSlot 线路发车时刻排班对象
     * @return     
     */
    @Transactional
    public int updateLineTimeSlot(LineTimeSlot lineTimeSlot,String token) throws Exception {     	
        return  this.lineTimeSlotMapper.updateLineTimeSlot(lineTimeSlot);
    }

    @Override
    public int updateLineTimeSlotSaleNum(LineTimeSlot lineTimeSlot, String token) throws Exception {
        return  this.lineTimeSlotMapper.updateLineTimeSlotSaleNum(lineTimeSlot);
    }

    /**
     * 查询线路发车时刻排班返回key-Value属性
     * @param lineTimeSlot 系统操作对象
     * @return     
     */
    public List<CommonKeyValue> optionsLineTimeSlot(LineTimeSlot lineTimeSlot,String token) throws Exception {
        List<LineTimeSlot> listLineTimeSlot=this.lineTimeSlotMapper.selectListLineTimeSlot(lineTimeSlot);
        List<CommonKeyValue> listOptions=new ArrayList<CommonKeyValue>();
        for(LineTimeSlot item:listLineTimeSlot){
            CommonKeyValue opts=new CommonKeyValue();
            opts.setId(""+item.getLtsId());
            opts.setValue(item.getOlId());
            listOptions.add(opts);
        }
        return listOptions;
    }
}
